package com.qianf.qfoffice.controller;

import com.qianf.qfoffice.entity.ResponseData;
import com.qianf.qfoffice.entity.Student;
import com.qianf.qfoffice.entity.User;
import com.qianf.qfoffice.service.StudentService;
import com.qianf.qfoffice.service.UserService;
import com.qianf.qfoffice.service.WeekService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;

@Controller
public class UserController {

    @Autowired
    private UserService userService;
    @Autowired
    private StudentService studentService;
    @Autowired
    private WeekService weekService;

    /**
     * 登录 接口
     * @param no
     * @param pass
     * @return
     */
    @RequestMapping("/login.do")
    @ResponseBody
    public ResponseData login(String no, String pass, HttpSession session){

        ResponseData<Object> responseData = new ResponseData<>();

        User user =  userService.findUserByNo(no);

        if (user==null){ // 该用户不存在
            responseData.setCode(1);
            responseData.setMsg("不存在该用户");
            return responseData;
        }

        if (!user.getPassword().equals(pass)){// 密码错误
            responseData.setCode(1);
            responseData.setMsg("密码或用户错误");
            return responseData;
        }

        session.setAttribute("user", user);
        // 登录成功
        responseData.setCode(0);
        responseData.setMsg("登录成功");

       return responseData;
    }


    /**
     * 判断当前用户是否 登录成功
     * @return
     */
    @RequestMapping("/LoginToJudge.do")
    @ResponseBody
    public ResponseData loginToJudge(HttpSession session){

        ResponseData<Object> responseData = new ResponseData<>();

        User user = (User) session.getAttribute("user");
        if (user!=null){//当前会话恢复登录过
            responseData.setCode(0);
            responseData.setMsg("登录成功");
        }else {
            responseData.setCode(1);
            responseData.setMsg("尚未登录");
        }

        return responseData;
    }

    /**
     * 获取当前用户信息
     * @param session
     * @return
     */
    @RequestMapping("/findByNo.do")
    @ResponseBody
    public ResponseData<User> findCurrentUser(HttpSession session){

        ResponseData<User> userResponseData = new ResponseData<>();
        User user = (User) session.getAttribute("user");

        userResponseData.setCode(0);
        userResponseData.setData(user);

        return userResponseData;
    }


    @RequestMapping("/loginOut.do")
    @ResponseBody
    public ResponseData loginOut (HttpSession session) {

        ResponseData<Object> responseData = new ResponseData<>();

        session.invalidate();//清除session信息 退出登录

        responseData.setCode(0);
        responseData.setMsg("退出登录成功");

        return responseData;


    }






    @RequestMapping("user/queryStudent.do")
    @ResponseBody
    public ResponseData<Student> queryStudent (HttpSession session) {

        User user = (User) session.getAttribute("user");

        ResponseData<Student> responseData = new ResponseData<>();

        Student studentByUno = studentService.findStudentByUno(user.getNo());

        responseData.setCode(1);
        responseData.setInfo(studentByUno);

        return responseData;

    }
//
//
//    @RequestMapping("week/queryWeek.do")
//    @ResponseBody
//    public Map<String,Object> queryWeek(Integer page,Integer limit) {
//
//        HashMap<String, Object> hashMap = new HashMap<>();
//
//        List<Week> allWeek = weekService.findAllWeek(page, limit);
//
//        long total = ((Page)allWeek).getTotal();
//
//        hashMap.put("code",0);
//        hashMap.put("msg","");
//        hashMap.put("count",total);
//        hashMap.put("data",allWeek);
//
//        return hashMap;
//
//    }
//
//
//    @RequestMapping("OaNew/week/add.do")
//    @ResponseBody
//    public void query (String title,String content,HttpSession session) {
//
//        Week week = new Week();
//        User user = (User) session.getAttribute("user");
//        week.setTitle(title);
//        week.setContent(content);
//        week.setUno(user.getNo());
//
//        int i = weekService.addWeek(week);
//
//    }
}
